#include "wuXMLkeyManager.h"


wuXMLkeyManager::wuXMLkeyManager()
{

}

wuXMLkeyManager::~wuXMLkeyManager()
{
}


void wuXMLkeyManager::loadXML(string _filename, bool _bLoop)
{

	bLoop = _bLoop;
	current_frame = -1;

	clear();

	if( XML.loadFile(_filename) ){
		cout << "xml loaded";
	}else{
		cout << "unable to load xml. check data/ folder";
		return;
	}

	// add info to filmInfo
//	num_pieces	= XML.getValue("PIECES"); // <PIECES> 5 </PIECES>
//	num_frames	= XML.getValue("FRAMES"); // <FRAMES> 340 </FRAMES>

//		//lets see how many <FRAME> </FRAME> tags there are in the xml file
	int num_frames = XML.getNumTags("FRAME:PT");

	cout << "FRAMES=" << num_frames << endl;

	for(int j = 0; j < num_frames; j++){

			//we push into the last STROKE tag
			//this temporarirly treats the tag as
			//the document root.
			XML.pushTag("FRAME", j);

			//we see how many points we have stored in <PT> tags
			int num_pieces = XML.getNumTags("PT");
//            cout << "XML_NUM_PIECES_" << num_pieces << endl;

            bool key = XML.getValue("PT:key", 0, 0);
				// add frame to filmInfo
            filmInfo.push_back(key);

			//this pops us out of the STROKE tag
			//sets the root back to the xml document
			XML.popTag();
	}
}

bool wuXMLkeyManager::getFrame(int i)
{
	return filmInfo[i];
}

bool wuXMLkeyManager::getNextFrame()
{
	current_frame++;
	if(current_frame >= filmInfo.size())
	{
		if(bLoop)
			current_frame = 1;
		else
			current_frame = filmInfo.size()-1;
	}

	return filmInfo[current_frame];
}

bool wuXMLkeyManager::getPrevFrame()
{
	current_frame--;
	if(current_frame < 0)
			current_frame = 0;

	return filmInfo[current_frame];
}

void wuXMLkeyManager::addFrame (bool _key)
{
	filmInfo.push_back(_key);
	current_frame = filmInfo.size()-1;
}

void wuXMLkeyManager::setCurrentFrame(bool _key)
{
	filmInfo[current_frame]=_key;
}

void wuXMLkeyManager::deleteFrame (int i)
{
	// erase the i frame
	filmInfo.erase(filmInfo.begin()+i);
}
void wuXMLkeyManager::saveXML (string _filename)
{

	for(int j = 0; j < filmInfo.size(); j++){

		XMLsave.addTag("FRAME");

		XMLsave.pushTag("FRAME", j);

		// temporary frameInfo

		bool key = filmInfo[j];

		int tagNum = XMLsave.addTag("PT");
		XMLsave.setValue("PT:key", (int)key, tagNum);
		XMLsave.popTag(); // close FRAME

	}
	XMLsave.saveFile(_filename);

}

void wuXMLkeyManager::clear()
{
	filmInfo.clear();
}
int wuXMLkeyManager::getNumFrames()
{
	return filmInfo.size();
}
int wuXMLkeyManager::getCurrentFrame()
{
	return current_frame;
}

int wuXMLkeyManager::getPrevActiveFrame()
{
    int tmp_frame = current_frame;

    do
    {
        tmp_frame--;
    }
    while (tmp_frame>-1 && filmInfo[tmp_frame]==false);
	return tmp_frame;
}

int wuXMLkeyManager::getNextActiveFrame()
{
    int tmp_frame = current_frame;

    do
    {
        tmp_frame++;
   }
    while (filmInfo[tmp_frame]==false && tmp_frame<filmInfo.size());

	if(tmp_frame==filmInfo.size())
        tmp_frame=-1;

	return tmp_frame;
}

void wuXMLkeyManager::goto0()
{
	current_frame=-1;
}

void wuXMLkeyManager::gotoFrame(int _frame)
{
	current_frame=_frame;
}

void wuXMLkeyManager::draw(int _x, int _y)
{
    int inc = 5;
    int line = 0;

    ofFill();
	for(int i = 0; i < filmInfo.size(); i++){
	    line = (int)i/230;

	    if(i==current_frame)
	    {
            ofNoFill();
            ofSetColor(255,255,255);
            ofCircle(_x + ((i%230)*inc), _y+(10*line), 3);
            ofFill();
	    }

        if(filmInfo[i]==false)
            ofSetColor(22,22,22);
        else
            ofSetColor(255,0,0);
        ofCircle(_x + ((i%230)*inc), _y+(10*line), 2);
	}
}

